Programming Languages Section

Swift

Card image
Post by Amina Delali, September 17th,2021

Some Facts

Swift is a general programming language used for any type of application: system programming, mobile and desktop apps, as well as cloud computing.

It is powerful and easy to learn, designed to allow the creation of fast , safe and expressif software. It encapsulates modern programming concepts while being open source. Originally created for iOS systems (supports all Apple platforms), it is now available for windows and linux operating systems. Nevertheless, it performs better in native iOS development.



How to install it

  • on Windows:

    To install Swift on Windows, you have two options:

    1. Download the special edition Swift For Windows from the project’s webpage , install it by following the instructions displayed on the screen. After the installation you can use the software to compile and run your Swift code.
    2. In the second option, you simply follow the instructions available in the Using Downloads Section of the official Swift download page under the Windows section. You will find in this page 
      1. Links for the releases to download among other necessary tools: for instance you will have to install Visual Studio with some of its components. 
      2. Instructions for the installation.
  • on ubuntu :

    The following steps were applied on the Ubuntu 21.04 version using the downloads and prerequisites defined for the Ubuntu 20.04 version.

    1. install the dependencies: open the terminal and run the update command. After that, go to the official Swift downloads page in the using download section corresponding to Linux Operating System. Then from the first step of the installation steps, select and copy the installation command  of the dependencies corresponding to your Ubuntu version. I selected the command corresponding to the  version Ubuntu 20.04. Paste the copied command on the terminal after adding ‘sudo’ at the beginning of the command line then hit enter (you can use apt instead of apt-get): sudo apt update
      sudo apt install binutils git gnupg2 libc6-dev libcurl4 libedit2 libgcc-9-dev libpython2.7 libsqlite3-0 libstdc++-9-dev libxml2 libz3-dev pkg-config tzdata uuid-dev zlib1g-dev
    2. download the corresponding Swift archive file from the releases section of the Swift download page: select the desired version ( I selected the version Ubuntu 20.04 ) and copy its link address. Then on the terminal download this archive using the wget command with the copied link address: wget https://swift.org/builds/swift-5.4.3-release/ubuntu2004/swift-5.4.3-RELEASE/swift-5.4.3-RELEASE-ubuntu20.04.tar.gz
    3. extract the downloaded archive:

      tar -xvf swift-5.4.3-RELEASE-ubuntu20.04.tar.gz

    4. move the archive to /usr/share/swift folder

      sudo mv swift-5.4.3-RELEASE-ubuntu20.04 /usr/share/swift

    5.  add the path of the new Swift folder to the PATH variable: 

      echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrc
      source ~/.bashrc

    6. test your swift installation (launch the Swift REPL):

      swift

    7. if you get the error indicating that a python library (an so file) is missing, just locate the exact file from your system, copy it to the new Swift folder: for example, in my case the needed file was libpython3.8.so.1.0

      sudo cp /home/username/anaconda3/lib/libpython3.8.so.1.0 /usr/share/swift/usr/lib

    8. to check the installed Swift version, run the following command:

      swift --version

In the video below, the details of the installation of the Swiftlanguage on Ubuntu.

The Hello World Example

For your first Hello World Code in Swift (on Ubuntu), open the terminal, and follow these steps:

  1. run the following commands to create and initialize the Hello project:

    mkdir Hello
    cd Hello
    swift package init --type executable

  2. open the file main.swift available at Sources/Hello/main.swift (from the Hello project's folder), you will find this instruction already in the file:
    print("Hello, world!")
  3.  run the following command to compile your project:

    swift build

  4. to run the corresponding code, un the following command:

    .build/debug/Hello

For more details about the Hello World code, you can check the following page.

Additional Information

Something to say ?

If you want to add something about the Swift language or about this post, please feel free to do it by commenting below 🙂 .